home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 027a / improc42.zip / PRIMER.DOC < prev    next >
Text File  |  1993-03-15  |  23KB  |  477 lines

  1.  
  2.                       PRIMER.DOC for IMPROCES(c).
  3.           Copyright John Wagner 1991-93. All rights reserved.
  4. ========================================================================
  5.  
  6.                    An Image Processing and VGA Primer
  7.  
  8. This article may only be distributed as part of the IMPROCES, Image
  9. Processing Software package by John Wagner.  IMPROCES is used for the
  10. examples and it is assumed the reader has a copy of the program.  This
  11. article may not be reproduced in any manner without prior permission
  12. from the author.
  13.  
  14. ( 1) THE IMAGE AND THE SCREEN:
  15. Images are represented as a series of points on a surface of varying
  16. intensity.  For example, on a monochrome or black and white photograph,
  17. the points on the image are represented with varying shades of gray.  On
  18. a computer screen, these points are called pixels.  Pixels on the screen
  19. are mapped into a two dimensional coordinate system that starts at the
  20. top, left corner with the coordinate 0,0.  The coordinates in the X
  21. direction refer to pixels going in the right (horizontal) direction and
  22. coordinates in the Y direction refer to pixels going in the down
  23. (vertical) direction.  The coordinates X,Y are used to define a specific
  24. pixel on the screen.  When a computer image is said to have a resolution
  25. of 320x200x256, it means that the image has an X width of 320 pixels, a
  26. Y length of 200 pixels and contains 256 colors.
  27.  
  28.            Diagram 1: Pixel Mapping on 320x200 video screen:
  29.  
  30.                            0,0          319,0
  31.                               X-------->
  32.                               Y
  33.                               |
  34.                               |
  35.                               V
  36.                            0,199        319,199
  37.  
  38. ( 2) COLOR:
  39. A shade of gray is defined as having equal levels of Red, Green and Blue
  40. (RGB).  A color image, however, is represented as having points that are
  41. represented by varying levels of RGB.  NOTE: The color model of RGB is
  42. just one way of representing color.  Conveniently it is also the model
  43. that computers use when representing colors on a screen.  For that
  44. reason, it will be the model we will use here!
  45.  
  46. When splitting up a color into it's RGB components, it is common to use a
  47. number between 0 and 1 (or percentage of total color) to represent the
  48. colors RGB intensities.  R=0, G=0, B=0 (usually shown as 0,0,0) would be
  49. the absence of all color (black) and 1,1,1 would be full intensity for
  50. all colors (white).  .5,0,0 would be half red, while 0,.25,0 would
  51. be one quarter green.  Using this model, an infinite quantity of colors are
  52. possible.  Unfortunately, personal computers of this day and age are not
  53. capable of handling an infinite quantity of colors and must use
  54. approximations when dealing with color.
  55.  
  56. ( 3) THE VGA:
  57. With the advent of the VGA video subsystem for IBM PC's and compatibles,
  58. Image Processing is now available to the home PC graphics enthusiast. It
  59. will help to understand the limitations of the hardware we are working
  60. with.  The VGA can display 256 colors at one time out of a possible
  61. 262,144.  The number 262,144 comes from the limitations of the VGA
  62. hardware itself.  The VGA (and many common graphics subsystems)
  63. represent their 256 colors in a lookup table of RGB values so that the
  64. memory where the video display is mapped need only keep track of one
  65. number (the lookup value) instead of three (see diagram 2).  The VGA
  66. lookup table allows for 64 (0 to 63) levels of RGB for each color.  This
  67. means that 64 to the 3rd power of different colors are possible.
  68. 64^3=262,144.  Because only 64 levels of RGB are possible, it is only
  69. possible to represent 64 shades of gray with a VGA.  Remember that a
  70. shade of gray is defined as equal levels of RGB.  There is also a
  71. disparity in the common usage of values from 0 to 1 to define a level of
  72. RGB.  However, this problem is easily solved by dividing the VGA lookup
  73. table RGB number by 64 to get its proper percentage of the total color
  74. (for example, 32/64=.5 or 50% of total color).
  75.  
  76.            Diagram 2: Sample Color Lookup Table (LUT) for VGA
  77.  
  78.                           Color      R   G   B
  79.                             1        0   23  56
  80.                             2        34  24  45
  81.                             3        23  12  43
  82.                             ....
  83.                             254      13  32  43
  84.                             255      12  63  12
  85.  
  86. Another important aspect of the VGA is that it only allows up to 256
  87. colors to be displayed at one time.  There are now Super VGA cards that
  88. allow you to work in display resolutions up to 1024x768 with 256 colors.
  89. These Super VGA cards have more memory on board so they can handle the
  90. higher resolutions.  It is important to understand that the amount of
  91. memory on the video board will determine the highest resolution it can
  92. handle.
  93.  
  94. Video Memory is bitmapped.  In a 256 color mode, one pixel requires one
  95. byte of memory, as a byte can hold a value from 0 to 255.  VGA video
  96. memory begins at the hexidecimal address A0000000.  The VGA video memory
  97. area is 64K in length.  Because of the length of the VGA memory area,
  98. the maximum resolution a standard VGA card can achieve is 320x200x256.
  99. This is because 320 pixels (bytes) times 200 pixels (bytes) equals
  100. 64,000 bytes (62.5K) and fills the video memory area.
  101.  
  102.             Diagram 3:  Memory Address Allocation of Pixels
  103.  
  104.                          pixel 0,0───┐ 0,1  0,2
  105.                                      │  │    │
  106.              Memory Address A0000000 ┘  │    │
  107.                             A0000001 ───┘    │
  108.                             A0000002 ────────┘
  109.  
  110. But wait a second, how can we get video modes up to 1024x768?  If you
  111. want a resolution of 1024x768x1byte (256 colors), you require 1024x768
  112. bytes of video memory, 786,432 bytes, or 768K.  Because the architecture
  113. of the PC only allows for a maximum of 64K of VGA memory, a Super VGA
  114. card maintains its own pool of memory that is displayed to the screen
  115. and swaps memory in and out of the 64K "proper" VGA video memory address
  116. space so that programs can write to it.
  117.  
  118. ( 4) SOME INFORMATION ABOUT IMPROCES:
  119. IMPROCES image processing functions all work in a defined area called
  120. the WORK AREA.  The default work area starts at the top-left corner of
  121. the screen and ends at pixel 196 in the X (horizontal) direction and at
  122. pixel 165 in the Y (vertical) direction.  These aren't magical numbers.
  123. A friend of mine lent me a CCD device to capture grayscale images and
  124. process them with IMPROCES.  The size of the image of the CCD device
  125. output was, you guessed it, 196x165.  You can change the WORK AREA
  126. dimensions by selecting WORK AREA from the ENHANCE pull-down menu and
  127. then defining a new rectanglar area for IMPROCES to use.
  128.  
  129. ( 5) THE HISTOGRAM:
  130. With all of that out of the way, let's examine a few things we can learn
  131. from an image without actually modifying it.  A tool that is commonly
  132. used to determine the overall contrast of an image is the Histogram.  A
  133. histogram is defined as the measure of the distribution of a defined
  134. set. As you probably know, histograms are not unique to image
  135. processing.
  136.  
  137. The histogram takes a count of all the values on the image and displays
  138. them graphically.  When I say a count, I mean how many pixels that
  139. contain the color in LUT (LookUp Table) 0 through 255.  The count of a
  140. color is called the BIN of the color.
  141.  
  142. To display a histogram with IMPROCES, select AREA HISTO from the ENHANCE
  143. pull-down menu. The histogram is displayed from the left to the right
  144. starting at color 0 and working over a column at a time to color 255.
  145. The BIN's are displayed as lines going upward.  You can move the mouse
  146. to a desired BIN and click on that column to get the exact count for
  147. that column, which is shown in the lower right corner.  You can also
  148. press 'S' to save the histogram to an ASCII file which you can examine
  149. later.
  150.  
  151. Assuming that the image is a grayscale image, the histogram shows us
  152. the overall contrast of the image by how much of the grayscale is
  153. covered by the image.  A high contrast image will cover most of the
  154. grayscale while a low contrast image will only cover a small portion of
  155. the grayscale.
  156.  
  157.           Diagram 4: Examples of Histograms:
  158.  
  159.                           High Contrast Image:
  160.  
  161.                                         -100  x6
  162.                                │││      -
  163.                              │ ││││     -
  164.                             ││││││││    -
  165.                             │││││││││   -
  166.                            │││││││││││  -
  167.                            └┴┴┴┴┴┴┴┴┴┘  -0
  168.                           0          255
  169.  
  170.  
  171.                           Low Contrast Image:
  172.  
  173.                                            -100  x8
  174.                                   │││      -
  175.                                   ││││     -
  176.                                   ││││     -
  177.                                   ││││     -
  178.                                   ││││     -
  179.                               ────┴┴┴┴───  -0
  180.                               0          255
  181.  
  182. ( 6) CONTRAST ENHANCEMENT:
  183. Contrast enhancement is one of the easiest to understand of the image
  184. processing functions.  In IMPROCES, contrast stretching will only work
  185. properly on images with a grayscale palette.  Future versions of
  186. IMPROCES will probably allow for contrast stretching of color images.
  187. Contrast Stretching will take a portion of the grayscale and stretch it
  188. so that it covers a wider portion of the grayscale.  To do this, you
  189. must first define an area of the grayscale that you would like to
  190. stretch.  IMPROCES provides three ways to do this.  All of the methods
  191. use two variables, one called Low_CLIP (L_CLIP) and one called the
  192. High_CLIP (H_CLIP).  Depending on which method you use, the variables
  193. will be used in different ways.
  194.  
  195. When using the CNTR STRTCH method, the first BIN working up from 0 that
  196. contains more pixels then the value of L_CLIP will become the color 0
  197. (black), and any BINs below that value are set to 0 as well.  The first
  198. BIN working down from 255 that contains more pixels then the value of
  199. H_CLIP will become the value 255 (white) and any BINs above that will
  200. become 255 as well.  All of the BINs in between will be remapped between
  201. 0 and 255 by a ratio of where they were with respect to the original LOW
  202. and HIGH CLIP values.
  203.  
  204.                  Take the original low contrast image:
  205.  
  206.                           Low Contrast Image:
  207.  
  208.                                         -100  x8
  209.                                │││      -
  210.                                ││││     -
  211.                                ││││     -
  212.                                ││││     -
  213.                                ││││     -
  214.                            ────┴┴┴┴───  -0
  215.                           0          255
  216.  
  217.     L_CLIP and H_CLIP are both set to 30 so the L_CLIP and H_CLIP will
  218.     hit at these points:
  219.  
  220.                                         -100  x8
  221.                                │││      -
  222.                                ││││     -
  223.                                ││││     -
  224.                                ││││     -
  225.                                ││││     -
  226.                            ────┴┴┴┴───  -0
  227.                           0    |  |  255
  228.                           L_CLIP  H_CLIP
  229.  
  230.  
  231.         These BINs will now be reset to 0 and 255 and the BINs in
  232.         between are set in respect to their original locations to the
  233.         L_CLIP and H_CLIP BINs:
  234.  
  235.                        Constrast Stretched Image:
  236.  
  237.                                           -100  x8
  238.                              │  │   │     -
  239.                              │  │   │  │  -
  240.                              │  │   │  │  -
  241.                              │  │   │  │  -
  242.                              │  │   │  │  -
  243.                              └──┴───┴──┘  -0
  244.                             0          255
  245.                             L_CLIP  H_CLIP
  246.  
  247.         The resulting image will have it's contrast stretched across the
  248.         entire grayscale, resulting in a higher contrast image.
  249.  
  250. The standard CNTR STRCH works well for a lot of images.  The problem is,
  251. rarely will an image be spread so evenly across the grayscale to begin
  252. with. A lot of times there will be spikes in the histogram at either end
  253. that you might want to remove.
  254.  
  255.                          Histogram with spikes:
  256.  
  257.                                          -100  x6
  258.                               │   │  │   -
  259.                              ││  │││ │   -
  260.                              ││ ││││ │   -
  261.                              │││││││││   -
  262.                              │││││││││   -
  263.                             ─┴┴┴┴┴┴┴┴┴─  -0
  264.                            0          255
  265.  
  266. Using the standard contrast stretch, you would not be able to get over
  267. these spikes if you wanted to stretch the middle of the histogram.
  268. IMPROCES provides the CNTR VSTCH for this purpose.  This method uses the
  269. variables L_CLIP and H_CLIP to pick which BIN you want to be the L_CLIP
  270. and H_CLIP values, without regard to their values.  The only thing to
  271. remember is not to set the L_CLIP higher then the H_CLIP, otherwise the
  272. program will give you an error message.  Besides the difference in the
  273. way the program uses the variables, VSTCH works identical to STRCTH.
  274.  
  275. CNTR LSTRCH works the same as VSTCH in the respect that the variables
  276. are used to pick the L_CLIP and H_CLIP values.  The difference is that
  277. the BINs that are below the L_CLIP are not set to 0, they are left alone
  278. and the same for the BINs above the H_CLIP value are left alone, not set
  279. to 255.  Only the BINs in between L_CLIP and H_CLIP are stretched
  280. between 0 and 255.
  281.  
  282. ( 7) CONVOLUTION:
  283. Convolution is another common method of processing an image.  It
  284. determines a new value for each pixel by evaluating the values of the
  285. neighboring pixels.  The main points of convolution can be explained
  286. rather easily:
  287.  
  288.         A matrix, called a kernel, is defined with certain values.  The
  289.         kernel is then passed over the image from left to right, top to
  290.         bottom, with the value of the center pixel being replaced with
  291.         the sum of the products of the kernel values and the pixels
  292.         under them.
  293.  
  294. The dimensions of the kernel must be odd numbers so that there will be a
  295. center position to represent each target pixel.  IMPROCES uses a 3x3
  296. kernel:
  297.  
  298.                              ┌───┬───┬───┐
  299.                              ├───┼───┼───┤
  300.                              ├───┼───┼───┤
  301.                              └───┴───┴───┘
  302.  
  303. Values are assigned to the kernel depending on the required convolution:
  304.  
  305.                            Sharpening kernel:
  306.                                -1  -1  -1
  307.                                -1   9  -1
  308.                                -1  -1  -1
  309.  
  310. The kernel is passed over the image from left to right, top to bottom.
  311. The kernel values are multiplied, point by point with the pixels in the
  312. 3x3 section of the image under it.  The products are then summed and the
  313. middle pixel in the image under the kernel is then replaced with the new
  314. value.
  315.  
  316.                            Sharpening kernel:
  317.                                -1  -1  -1
  318.                                -1   9  -1
  319.                                -1  -1  -1
  320.  
  321.                Example of area of image being processed:
  322.                                23  34  25
  323.                                23  43  21
  324.                                23  43  43
  325.  
  326.                            Product of values:
  327.                               -23 -34 -25
  328.                               -23 387 -21
  329.                               -23 -43 -43
  330.  
  331.                             Sum of products:
  332.                                   156
  333.  
  334.                          Changed area of image:
  335.                                23  34  25
  336.                                23 156  21
  337.                                23  43  43
  338.  
  339. The new value of the center pixel is then written to the screen.  You
  340. should note that the output from the previous operation is not used for
  341. input in the next operation.  The input and output image must be treated
  342. separately.  IMPROCES does this operation in place by using two rotating
  343. three line buffers.  The resulting image is said to have convolved from
  344. the original.
  345.  
  346. ( 7)(a) LAPLACIAN KERNEL:
  347. The example shows a sharpening kernel.  A sharpening kernel is actually
  348. a Laplacian kernel with the original image added back in.  The Laplacian
  349. is an edge detecting kernel, so when you detect the edges of the image
  350. and then add the original image back in, you sharpen the edges, thereby
  351. improving the sharpness of the image.  A Laplacian kernel looks like so:
  352.  
  353.                            Laplacian kernel:
  354.                                -1  -1  -1
  355.                                -1   8  -1
  356.                                -1  -1  -1
  357.  
  358. You will notice that the center value for the Laplacian is an 8 and the
  359. sum of the kernel is 0.  What this does to an image is to make areas
  360. that have no real features and are close to being a continuous tone
  361. disappear and leave features that have a lot of contrast with their
  362. neighbors.  This is the function of an edge detector.  If the kernel has
  363. a sum of 0, it will enhance the edges of an image in a certain
  364. direction.  In the case of the Laplacian, the enhancement will take
  365. place in all directions.  Here is why this will happen:
  366.  
  367.         If you have a 3x3 area of the image in which pixels are equal to
  368.         the same value, for example 200, it will be uniform in color and
  369.         contain no edges.  When the Laplacian is passed over this
  370.         section, the result of the convolution will be 0, or the absence
  371.         of an edge.  If you have an area that looks like:
  372.  
  373.                               100 100 100
  374.                               200 200 200
  375.                               250 250 250
  376.  
  377.         The result of the convolution with the Laplacian would be 150,
  378.         and there would be an edge present.
  379.  
  380.         Take for example a run of pixels that looks like this: 0 150
  381.         200, that area obviously contains an edge.  On a graph it would
  382.         look like this:
  383.  
  384.                                  /|255
  385.                                /  |200
  386.                              /    |150
  387.                             0-----|
  388.  
  389.         If you used a 1x3 kernel with the values -1 0 1, the middle
  390.         value would become 200 ( (0x0=0) + (150x0=0) + (200x1=200)) and
  391.         show the precence of the edge.  If the values of the run were
  392.         100 100 100, the area would be uniform and the value of the
  393.         middle pixel would become 0 and accurately depict the uniform
  394.         area.
  395.  
  396. ( 7)(b) HORIZONTAL KERNEL:
  397. A horizontal kernel looks like this:
  398.  
  399.                            Horizontal kernel:
  400.                                -1  -1  -1
  401.                                 0   0   0
  402.                                 1   1   1
  403.  
  404. The horizontal kernel will only detect edges in the horizontal direction
  405. due to the direction of the kernel.
  406.  
  407. ( 7)(c) VERTICAL KERNEL:
  408. A vertical kernel looks like this:
  409.  
  410.                             Vertical kernel:
  411.                                -1   0   1
  412.                                -1   0   1
  413.                                -1   0   1
  414.  
  415. The vertical kernel will only detect edges in the vertical direction due
  416. to the direction of the kernel.
  417.  
  418. There are other methods of edge detecion available that IMPROCES does
  419. not implement at the present time.  In fact there are new methods and
  420. filters being invented every day.  Future versions of IMPROCES will
  421. support convolving an area with a separate filter for each direction and
  422. other forms of edge detection.
  423.  
  424. ( 7)(d) THE BOOST FUNCTION:
  425. There is also a variable called BOOST that is used when using the
  426. convolution filters.  BOOST is used to increase or decrease the amount
  427. of the filter that is applied to the original image.  What happens is
  428. the pixels under the kernel are first multiplied by the corresponding
  429. kernel value and then are multiplied by the BOOST value.  A BOOST value
  430. of less then 1.0 will lessen the effect of the filter, while a value of
  431. greater then 1.0 will increase the effect.
  432.  
  433. IMPROCES includes a Custom Filter function that lets you define the
  434. kernel to use.  You will also note that as of version 3.0 of IMPROCES,
  435. there are separate functions for grayscale images and for color images.
  436. The grayscale functions work a lot faster.  The color functions must
  437. convol the RGB attributes of each pixel and then search the palette for
  438. the proper color to replace the pixel with.  The color process rarely
  439. will find an exact match for the color that gets produced from the
  440. convolution, but it will find the closest possible match and use it. The
  441. gray functions need only convol the LUT value of the pixels and use the
  442. result to get an exact match.
  443.  
  444.  
  445. ( 7)(e) AVERAGE AND MEDIAN:
  446. Two other filters that are included are the Average and Median filters.
  447. Both of these use a 3x3 matrix as before, but only the values in the
  448. image are used.
  449.  
  450. Average will find the average (mean) value of the pixels under the
  451. matrix and replace the center pixel with that value. Median will find
  452. the middle value used and use that.  Both functions work the same for
  453. grayscale and color images.
  454.  
  455. An important note for all of the functions that use a matrix or a kernel
  456. is that the pixels on the edges (top, right, left, bottom) of the work
  457. area will not be changed by the functions.  This is because these pixels
  458. do not have enough neighbors to be used in processing.  In IMPROCES
  459. these pixels are simply left alone.
  460.  
  461. ( 8) CONCLUSION:
  462. Image Processing is used in many fields.  With the price of personal
  463. computers equipped with VGA and SVGA hardware dropping like a rock, and
  464. with software like IMPROCES available for only $30, Image Processing is
  465. now available to the masses.  It is exciting, useful, and most of all
  466. fun.
  467.  
  468. I hope this little primer has aroused your interest in Image Processing
  469. and that it helps make some things that IMPROCES can do a little
  470. clearer.  If for some reason you obtained this article without a copy of
  471. IMPROCES, the latest version of the program can be downloaded from the
  472. Dust Devil BBS in Las Vegas, Nevada, (702)796-7134.  I can also be
  473. reached at the Dust Devil BBS if you have any questions about IMPROCES.
  474.  
  475. John Wagner
  476. [PRIMER.DOC revised November 1992]
  477.